<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
        "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <link href="../Styles/style.css" rel="stylesheet" type="text/css"/>


</head>

<body>
<div class="article-body">
    <div class="article-intro">

        <h1>Java 方法</h1>
        <p>
            在前面几个章节中我们经常使用到 <strong>System.out.println()</strong>，那么它是什么呢？
        </p>
        <ul><li>println() 是一个方法。</li><li>
            System 是系统类。</li><li>
            out 是标准输出对象。</li></ul>

        这句话的用法是调用系统类 System 中的标准输出对象 out 中的方法 println()。
        <p></p>
        <h3>那么什么是方法呢？</h3>
        <p>Java方法是语句的集合，它们在一起执行一个功能。</p>
        <ul>
            <li>方法是解决一类问题的步骤的有序组合</li>
            <li>方法包含于类或对象中</li>
            <li>方法在程序中被创建，在其他地方被引用</li>
        </ul>
        <h3>方法的优点</h3>

        <ul><li>
            1. 使程序变得更简短而清晰。</li><li>
            2. 有利于程序维护。</li><li>
            3. 可以提高程序开发的效率。</li><li>
            4. 提高了代码的重用性。</li></ul>

        <h3>方法的命名规则
        </h3><ul><li>
        1.方法的名字的第一个单词应以小写字母作为开头，后面的单词则用大写字母开头写，不使用连接符。例如：<span class="marked">addPerson</span>。</li><li>
        2.下划线可能出现在 JUnit 测试方法名称中用以分隔名称的逻辑组件。一个典型的模式是：<span class="marked">test&lt;MethodUnderTest&gt;_&lt;state&gt;</span>，例如 <span class="marked">testPop_emptyStack</span>。 </li></ul>


        <hr>
        <h2>方法的定义</h2>
        <p>一般情况下，定义一个方法包含以下语法：</p>
        <div class="example">
            <div class="example_code">
                <pre class="hl-main"><span class="hl-code">修饰符 返回值类型 方法名</span><span class="hl-brackets">(</span><span class="hl-code">参数类型 参数名</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
    ...
    方法体
    ...
    </span><span class="hl-reserved">return</span><span class="hl-code"> 返回值;
</span><span class="hl-brackets">}</span></pre>
            </div>
        </div>
        <p>方法包含一个方法头和一个方法体。下面是一个方法的所有部分：</p>
        <ul>
            <li><strong>修饰符：</strong>修饰符，这是可选的，告诉编译器如何调用该方法。定义了该方法的访问类型。</li>
            <li><strong>返回值类型 ：</strong>方法可能会返回值。returnValueType 是方法返回值的数据类型。有些方法执行所需的操作，但没有返回值。在这种情况下，returnValueType 是关键字<strong>void</strong>。</li>
            <li><strong>方法名：</strong>是方法的实际名称。方法名和参数表共同构成方法签名。</li>
            <li><strong>参数类型：</strong>参数像是一个占位符。当方法被调用时，传递值给参数。这个值被称为实参或变量。参数列表是指方法的参数类型、顺序和参数的个数。参数是可选的，方法可以不包含任何参数。</li>
            <li><strong>方法体：</strong>方法体包含具体的语句，定义该方法的功能。</li>
        </ul>
        <p>
            <img src="http://www.runoob.com/wp-content/uploads/2013/12/12-130Q1220955916.jpg">
        </p>
        <p>如：</p>
        <div class="example">
            <div class="example_code">
                <pre class="hl-main"><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">age</span><span class="hl-brackets">(</span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">birthday</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">...</span><span class="hl-brackets">}</span></pre>
            </div>
        </div>
        <p>
            参数可以有多个：
        </p><div class="example">
        <div class="example_code">
            <pre class="hl-main"><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">float</span><span class="hl-code"> </span><span class="hl-identifier">interest</span><span class="hl-brackets">(</span><span class="hl-types">float</span><span class="hl-code"> </span><span class="hl-identifier">principal</span><span class="hl-code">, </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">year</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">...</span><span class="hl-brackets">}</span></pre>
        </div>
    </div>
        <p>
            <strong>注意：</strong> 在一些其它语言中方法指过程和函数。一个返回非void类型返回值的方法称为函数；一个返回void类型返回值的方法叫做过程。
        </p>
        <h3>实例</h3>
        <p>
            下面的方法包含 2 个参数 num1 和 num2，它返回这两个参数的最大值。
        </p>

        <div class="example">
            <div class="example_code">
                <pre class="hl-main"><span class="hl-comment">/*</span><span class="hl-comment">* 返回两个整型变量数据的较大值 </span><span class="hl-comment">*/</span><span class="hl-code">
</span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">max</span><span class="hl-brackets">(</span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">num1</span><span class="hl-code">, </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">num2</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
   </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">result</span><span class="hl-code">;
   </span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">num1</span><span class="hl-code"> &gt; </span><span class="hl-identifier">num2</span><span class="hl-brackets">)</span><span class="hl-code">
      </span><span class="hl-identifier">result</span><span class="hl-code"> = </span><span class="hl-identifier">num1</span><span class="hl-code">;
   </span><span class="hl-reserved">else</span><span class="hl-code">
      </span><span class="hl-identifier">result</span><span class="hl-code"> = </span><span class="hl-identifier">num2</span><span class="hl-code">;

   </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-identifier">result</span><span class="hl-code">;
</span><span class="hl-brackets">}</span></pre>
            </div>
        </div>
        <hr>
        <h2>方法调用
        </h2>
        <p>Java 支持两种调用方法的方式，根据方法是否返回值来选择。
        </p><p>当程序调用一个方法时，程序的控制权交给了被调用的方法。当被调用方法的返回语句执行或者到达方法体闭括号时候交还控制权给程序。
    </p><p>当方法返回一个值的时候，方法调用通常被当做一个值。例如：
    </p><div class="example">
        <div class="example_code">
            <pre class="hl-main"><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">larger</span><span class="hl-code"> = </span><span class="hl-identifier">max</span><span class="hl-brackets">(</span><span class="hl-number">30</span><span class="hl-code">, </span><span class="hl-number">40</span><span class="hl-brackets">)</span><span class="hl-code">;</span></pre>
        </div>
    </div>
        <p>如果方法返回值是void，方法调用一定是一条语句。例如，方法println返回void。下面的调用是个语句：
        </p>
        <div class="example">
            <div class="example_code">
                <pre class="hl-main"><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">欢迎访问菜鸟教程！</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;</span></pre>
            </div>
        </div>

        <h3>示例</h3>
        <p>下面的例子演示了如何定义一个方法，以及如何调用它：</p>
        <div class="example">
            <h2 class="example">TestMax.java 文件代码：</h2>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">TestMax</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
   </span><span class="hl-comment">/*</span><span class="hl-comment">* 主方法 </span><span class="hl-comment">*/</span><span class="hl-code">
   </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-brackets">[</span><span class="hl-brackets">]</span><span class="hl-code"> </span><span class="hl-identifier">args</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
      </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">i</span><span class="hl-code"> = </span><span class="hl-number">5</span><span class="hl-code">;
      </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">j</span><span class="hl-code"> = </span><span class="hl-number">2</span><span class="hl-code">;
      </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">k</span><span class="hl-code"> = </span><span class="hl-identifier">max</span><span class="hl-brackets">(</span><span class="hl-identifier">i</span><span class="hl-code">, </span><span class="hl-identifier">j</span><span class="hl-brackets">)</span><span class="hl-code">;
      </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-identifier">i</span><span class="hl-code"> + </span><span class="hl-quotes">"</span><span class="hl-string"> 和 </span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">j</span><span class="hl-code"> + </span><span class="hl-quotes">"</span><span class="hl-string"> 比较，最大值是：</span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">k</span><span class="hl-brackets">)</span><span class="hl-code">;
   </span><span class="hl-brackets">}</span><span class="hl-code">

   </span><span class="hl-comment">/*</span><span class="hl-comment">* 返回两个整数变量较大的值 </span><span class="hl-comment">*/</span><span class="hl-code">
   </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">max</span><span class="hl-brackets">(</span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">num1</span><span class="hl-code">, </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">num2</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
      </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">result</span><span class="hl-code">;
      </span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">num1</span><span class="hl-code"> &gt; </span><span class="hl-identifier">num2</span><span class="hl-brackets">)</span><span class="hl-code">
         </span><span class="hl-identifier">result</span><span class="hl-code"> = </span><span class="hl-identifier">num1</span><span class="hl-code">;
      </span><span class="hl-reserved">else</span><span class="hl-code">
         </span><span class="hl-identifier">result</span><span class="hl-code"> = </span><span class="hl-identifier">num2</span><span class="hl-code">;

      </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-identifier">result</span><span class="hl-code">;
   </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span></pre>
            </div>
        </div>
        <p>以上实例编译运行结果如下：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="lit">5</span><span class="pln"> </span><span class="pun">和</span><span class="pln"> </span><span class="lit">2</span><span class="pln"> </span><span class="pun">比较，最大值是：</span><span class="lit">5</span></pre>
        <p>这个程序包含 main 方法和 max 方法。main 方法是被 JVM 调用的，除此之外，main 方法和其它方法没什么区别。
        </p><p>main 方法的头部是不变的，如例子所示，带修饰符 public 和 static,返回 void 类型值，方法名字是 main,此外带个一个 String[] 类型参数。String[] 表明参数是字符串数组。
    </p>
        <hr>
        <h2>void 关键字</h2>
        <p>本节说明如何声明和调用一个 void 方法。
        </p><p>下面的例子声明了一个名为 printGrade 的方法，并且调用它来打印给定的分数。
    </p><h3>示例</h3>
        <div class="example">
            <h2 class="example">TestVoidMethod.java 文件代码：</h2>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">TestVoidMethod</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
  </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-brackets">[</span><span class="hl-brackets">]</span><span class="hl-code"> </span><span class="hl-identifier">args</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-identifier">printGrade</span><span class="hl-brackets">(</span><span class="hl-number">78</span><span class="hl-number">.5</span><span class="hl-brackets">)</span><span class="hl-code">;
  </span><span class="hl-brackets">}</span><span class="hl-code">

  </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">printGrade</span><span class="hl-brackets">(</span><span class="hl-types">double</span><span class="hl-code"> </span><span class="hl-identifier">score</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">score</span><span class="hl-code"> &gt;= </span><span class="hl-number">90</span><span class="hl-number">.0</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
       </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">A</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-brackets">}</span><span class="hl-code">
    </span><span class="hl-reserved">else</span><span class="hl-code"> </span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">score</span><span class="hl-code"> &gt;= </span><span class="hl-number">80</span><span class="hl-number">.0</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
       </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">B</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-brackets">}</span><span class="hl-code">
    </span><span class="hl-reserved">else</span><span class="hl-code"> </span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">score</span><span class="hl-code"> &gt;= </span><span class="hl-number">70</span><span class="hl-number">.0</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
       </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">C</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-brackets">}</span><span class="hl-code">
    </span><span class="hl-reserved">else</span><span class="hl-code"> </span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">score</span><span class="hl-code"> &gt;= </span><span class="hl-number">60</span><span class="hl-number">.0</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
       </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">D</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-brackets">}</span><span class="hl-code">
    </span><span class="hl-reserved">else</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
       </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">'</span><span class="hl-string">F</span><span class="hl-quotes">'</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-brackets">}</span><span class="hl-code">
  </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span></pre>
            </div>
        </div>
        <p>以上实例编译运行结果如下：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">C</span></pre>
        <p>这里printGrade方法是一个void类型方法，它不返回值。
        </p><p>一个void方法的调用一定是一个语句。 所以，它被在main方法第三行以语句形式调用。就像任何以分号结束的语句一样。
    </p>
        <hr><h2>通过值传递参数</h2>
        <p>
            调用一个方法时候需要提供参数，你必须按照参数列表指定的顺序提供。

        </p><p>例如，下面的方法连续n次打印一个消息：
    </p>

        <div class="example">
            <h2 class="example">TestVoidMethod.java 文件代码：</h2>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">nPrintln</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">message</span><span class="hl-code">, </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">n</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
  </span><span class="hl-reserved">for</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">i</span><span class="hl-code"> = </span><span class="hl-number">0</span><span class="hl-code">; </span><span class="hl-identifier">i</span><span class="hl-code"> &lt; </span><span class="hl-identifier">n</span><span class="hl-code">; </span><span class="hl-identifier">i</span><span class="hl-code">++</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-identifier">message</span><span class="hl-brackets">)</span><span class="hl-code">;
  </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span></pre>
            </div>
        </div>
        <h3>示例</h3>
        <p>
            下面的例子演示按值传递的效果。</p><p>
        该程序创建一个方法，该方法用于交换两个变量。
    </p>
        <div class="example">
            <h2 class="example">TestPassByValue.java 文件代码：</h2>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">TestPassByValue</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
  </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-brackets">[</span><span class="hl-brackets">]</span><span class="hl-code"> </span><span class="hl-identifier">args</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">num1</span><span class="hl-code"> = </span><span class="hl-number">1</span><span class="hl-code">;
    </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">num2</span><span class="hl-code"> = </span><span class="hl-number">2</span><span class="hl-code">;

    </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">交换前 num1 的值为：</span><span class="hl-quotes">"</span><span class="hl-code"> +
                        </span><span class="hl-identifier">num1</span><span class="hl-code"> + </span><span class="hl-quotes">"</span><span class="hl-string"> ，num2 的值为：</span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">num2</span><span class="hl-brackets">)</span><span class="hl-code">;

    </span><span class="hl-comment">//</span><span class="hl-comment"> 调用swap方法</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-identifier">swap</span><span class="hl-brackets">(</span><span class="hl-identifier">num1</span><span class="hl-code">, </span><span class="hl-identifier">num2</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">交换后 num1 的值为：</span><span class="hl-quotes">"</span><span class="hl-code"> +
                       </span><span class="hl-identifier">num1</span><span class="hl-code"> + </span><span class="hl-quotes">"</span><span class="hl-string"> ，num2 的值为：</span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">num2</span><span class="hl-brackets">)</span><span class="hl-code">;
  </span><span class="hl-brackets">}</span><span class="hl-code">
  </span><span class="hl-comment">/*</span><span class="hl-comment">* 交换两个变量的方法 </span><span class="hl-comment">*/</span><span class="hl-code">
  </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">swap</span><span class="hl-brackets">(</span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">n1</span><span class="hl-code">, </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">n2</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-special">\t</span><span class="hl-string">进入 swap 方法</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-special">\t</span><span class="hl-special">\t</span><span class="hl-string">交换前 n1 的值为：</span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">n1</span><span class="hl-code">
                         + </span><span class="hl-quotes">"</span><span class="hl-string">，n2 的值：</span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">n2</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-comment">//</span><span class="hl-comment"> 交换 n1 与 n2的值</span><span class="hl-comment"></span><span class="hl-code">
    </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">temp</span><span class="hl-code"> = </span><span class="hl-identifier">n1</span><span class="hl-code">;
    </span><span class="hl-identifier">n1</span><span class="hl-code"> = </span><span class="hl-identifier">n2</span><span class="hl-code">;
    </span><span class="hl-identifier">n2</span><span class="hl-code"> = </span><span class="hl-identifier">temp</span><span class="hl-code">;

    </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-special">\t</span><span class="hl-special">\t</span><span class="hl-string">交换后 n1 的值为 </span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">n1</span><span class="hl-code">
                         + </span><span class="hl-quotes">"</span><span class="hl-string">，n2 的值：</span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">n2</span><span class="hl-brackets">)</span><span class="hl-code">;
  </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span></pre>
            </div>
        </div>
        <p>以上实例编译运行结果如下：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pun">交换前</span><span class="pln"> num1 </span><span class="pun">的值为：</span><span class="lit">1</span><span class="pln"> </span><span class="pun">，</span><span class="pln">num2 </span><span class="pun">的值为：</span><span class="lit">2</span><span class="pln">
&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="pun">进入</span><span class="pln"> swap </span><span class="pun">方法</span><span class="pln">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="pun">交换前</span><span class="pln"> n1 </span><span class="pun">的值为：</span><span class="lit">1</span><span class="pun">，</span><span class="pln">n2 </span><span class="pun">的值：</span><span class="lit">2</span><span class="pln">
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span class="pun">交换后</span><span class="pln"> n1 </span><span class="pun">的值为</span><span class="pln"> </span><span class="lit">2</span><span class="pun">，</span><span class="pln">n2 </span><span class="pun">的值：</span><span class="lit">1</span><span class="pln">
</span><span class="pun">交换后</span><span class="pln"> num1 </span><span class="pun">的值为：</span><span class="lit">1</span><span class="pln"> </span><span class="pun">，</span><span class="pln">num2 </span><span class="pun">的值为：</span><span class="lit">2</span></pre>
        <p>
            传递两个参数调用swap方法。有趣的是，方法被调用后，实参的值并没有改变。
        </p>
        <hr>
        <h2>方法的重载</h2>
        <p>
            上面使用的max方法仅仅适用于int型数据。但如果你想得到两个浮点类型数据的最大值呢？
        </p><p>解决方法是创建另一个有相同名字但参数不同的方法，如下面代码所示：
    </p>
        <div class="example">
            <div class="example_code">
                <pre class="hl-main"><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">double</span><span class="hl-code"> </span><span class="hl-identifier">max</span><span class="hl-brackets">(</span><span class="hl-types">double</span><span class="hl-code"> </span><span class="hl-identifier">num1</span><span class="hl-code">, </span><span class="hl-types">double</span><span class="hl-code"> </span><span class="hl-identifier">num2</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
  </span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">num1</span><span class="hl-code"> &gt; </span><span class="hl-identifier">num2</span><span class="hl-brackets">)</span><span class="hl-code">
    </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-identifier">num1</span><span class="hl-code">;
  </span><span class="hl-reserved">else</span><span class="hl-code">
    </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-identifier">num2</span><span class="hl-code">;
</span><span class="hl-brackets">}</span></pre>
            </div>
        </div>
        <p>如果你调用max方法时传递的是int型参数，则 int型参数的max方法就会被调用；
        </p><p>如果传递的是double型参数，则double类型的max方法体会被调用，这叫做方法重载；
    </p><p>就是说一个类的两个方法拥有相同的名字，但是有不同的参数列表。
    </p><p>Java编译器根据方法签名判断哪个方法应该被调用。
    </p><p>方法重载可以让程序更清晰易读。执行密切相关任务的方法应该使用相同的名字。
    </p><p>重载的方法必须拥有不同的参数列表。你不能仅仅依据修饰符或者返回类型的不同来重载方法。
    </p><hr><h2>变量作用域</h2>
        <p>
            变量的范围是程序中该变量可以被引用的部分。
        </p><p>方法内定义的变量被称为局部变量。
    </p><p>局部变量的作用范围从声明开始，直到包含它的块结束。
    </p><p>局部变量必须声明才可以使用。
    </p><p>方法的参数范围涵盖整个方法。参数实际上是一个局部变量。
    </p><p>for循环的初始化部分声明的变量，其作用范围在整个循环。
    </p><p>但循环体内声明的变量其适用范围是从它声明到循环体结束。它包含如下所示的变量声明：
    </p>
        <p>
            <img src="http://www.runoob.com/wp-content/uploads/2013/12/12-130Q1221013F0.jpg">
        </p>
        <p>你可以在一个方法里，不同的非嵌套块中多次声明一个具有相同的名称局部变量，但你不能在嵌套块内两次声明局部变量。
        </p>
        <h2>命令行参数的使用</h2>
        <p>
            有时候你希望运行一个程序时候再传递给它消息。这要靠传递命令行参数给main()函数实现。
        </p><p>命令行参数是在执行程序时候紧跟在程序名字后面的信息。</p>
        <h3>实例</h3><p>
        下面的程序打印所有的命令行参数：
    </p>
        <div class="example">
            <h2 class="example">CommandLine.java 文件代码：</h2>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">CommandLine</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
   </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">args</span><span class="hl-brackets">[</span><span class="hl-brackets">]</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
      </span><span class="hl-reserved">for</span><span class="hl-brackets">(</span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">i</span><span class="hl-code">=</span><span class="hl-number">0</span><span class="hl-code">; </span><span class="hl-identifier">i</span><span class="hl-code">&lt;</span><span class="hl-identifier">args</span><span class="hl-code">.</span><span class="hl-identifier">length</span><span class="hl-code">; </span><span class="hl-identifier">i</span><span class="hl-code">++</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
         </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">args[</span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">i</span><span class="hl-code"> + </span><span class="hl-quotes">"</span><span class="hl-string">]: </span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">args</span><span class="hl-brackets">[</span><span class="hl-identifier">i</span><span class="hl-brackets">]</span><span class="hl-brackets">)</span><span class="hl-code">;
      </span><span class="hl-brackets">}</span><span class="hl-code">
   </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span></pre>
            </div>
        </div>
        <p>
            如下所示，运行这个程序：
        </p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">$ javac </span><span class="typ">CommandLine</span><span class="pun">.</span><span class="pln">java
$ java </span><span class="typ">CommandLine</span><span class="pln"> </span><span class="kwd">this</span><span class="pln"> </span><span class="kwd">is</span><span class="pln"> a command line </span><span class="lit">200</span><span class="pln"> </span><span class="pun">-</span><span class="lit">100</span><span class="pln">
args</span><span class="pun">[</span><span class="lit">0</span><span class="pun">]:</span><span class="pln"> </span><span class="kwd">this</span><span class="pln">
args</span><span class="pun">[</span><span class="lit">1</span><span class="pun">]:</span><span class="pln"> </span><span class="kwd">is</span><span class="pln">
args</span><span class="pun">[</span><span class="lit">2</span><span class="pun">]:</span><span class="pln"> a
args</span><span class="pun">[</span><span class="lit">3</span><span class="pun">]:</span><span class="pln"> command
args</span><span class="pun">[</span><span class="lit">4</span><span class="pun">]:</span><span class="pln"> line
args</span><span class="pun">[</span><span class="lit">5</span><span class="pun">]:</span><span class="pln"> </span><span class="lit">200</span><span class="pln">
args</span><span class="pun">[</span><span class="lit">6</span><span class="pun">]:</span><span class="pln"> </span><span class="pun">-</span><span class="lit">100</span></pre>
        <hr><h2>构造方法</h2>
        <p>
            当一个对象被创建时候，构造方法用来初始化该对象。构造方法和它所在类的名字相同，但构造方法没有返回值。
        </p><p>通常会使用构造方法给一个类的实例变量赋初值，或者执行其它必要的步骤来创建一个完整的对象。
    </p><p>不管你与否自定义构造方法，所有的类都有构造方法，因为Java自动提供了一个默认构造方法，它把所有成员初始化为0。
    </p><p>一旦你定义了自己的构造方法，默认构造方法就会失效。
    </p><h3>实例</h3>
        <p>
            下面是一个使用构造方法的例子：
        </p>
        <div class="example">
            <div class="example_code">
                <pre class="hl-main"><span class="hl-comment">//</span><span class="hl-comment"> 一个简单的构造函数</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">MyClass</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
  </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">x</span><span class="hl-code">;

  </span><span class="hl-comment">//</span><span class="hl-comment"> 以下是构造函数</span><span class="hl-comment"></span><span class="hl-code">
  </span><span class="hl-identifier">MyClass</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-identifier">x</span><span class="hl-code"> = </span><span class="hl-number">10</span><span class="hl-code">;
  </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span></pre>
            </div>
        </div>
        <p>你可以像下面这样调用构造方法来初始化一个对象：</p>
        <div class="example">
            <h2 class="example">ConsDemo.java 文件代码：</h2>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">ConsDemo</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
   </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">args</span><span class="hl-brackets">[</span><span class="hl-brackets">]</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
      </span><span class="hl-identifier">MyClass</span><span class="hl-code"> </span><span class="hl-identifier">t1</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">MyClass</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
      </span><span class="hl-identifier">MyClass</span><span class="hl-code"> </span><span class="hl-identifier">t2</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">MyClass</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
      </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-identifier">t1</span><span class="hl-code">.</span><span class="hl-identifier">x</span><span class="hl-code"> + </span><span class="hl-quotes">"</span><span class="hl-string"> </span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">t2</span><span class="hl-code">.</span><span class="hl-identifier">x</span><span class="hl-brackets">)</span><span class="hl-code">;
   </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span></pre>
            </div>
        </div>
        <p>大多时候需要一个有参数的构造方法。
        </p>
        <h3>实例</h3>
        <p>下面是一个使用构造方法的例子：</p>
        <div class="example">
            <div class="example_code">
                <pre class="hl-main"><span class="hl-comment">//</span><span class="hl-comment"> 一个简单的构造函数</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">MyClass</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
  </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">x</span><span class="hl-code">;

  </span><span class="hl-comment">//</span><span class="hl-comment"> 以下是构造函数</span><span class="hl-comment"></span><span class="hl-code">
  </span><span class="hl-identifier">MyClass</span><span class="hl-brackets">(</span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">i</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-identifier">x</span><span class="hl-code"> = </span><span class="hl-identifier">i</span><span class="hl-code">;
  </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span></pre>
            </div>
        </div>
        <p>你可以像下面这样调用构造方法来初始化一个对象：</p>
        <div class="example">
            <h2 class="example">ConsDemo.java 文件代码：</h2>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">ConsDemo</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
  </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">args</span><span class="hl-brackets">[</span><span class="hl-brackets">]</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-identifier">MyClass</span><span class="hl-code"> </span><span class="hl-identifier">t1</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">MyClass</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-number">10</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">MyClass</span><span class="hl-code"> </span><span class="hl-identifier">t2</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">MyClass</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-number">20</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-identifier">t1</span><span class="hl-code">.</span><span class="hl-identifier">x</span><span class="hl-code"> + </span><span class="hl-quotes">"</span><span class="hl-string"> </span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">t2</span><span class="hl-code">.</span><span class="hl-identifier">x</span><span class="hl-brackets">)</span><span class="hl-code">;
  </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span></pre>
            </div>
        </div>
        <p>运行结果如下：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="lit">10</span><span class="pln"> </span><span class="lit">20</span></pre>
        <h2>可变参数</h2>

        <p>JDK 1.5 开始，Java支持传递同类型的可变参数给一个方法。
        </p><p>方法的可变参数的声明如下所示：</p>
        <div class="example">
            <div class="example_code">
                <pre class="hl-main"><span class="hl-identifier">typeName</span><span class="hl-code">... </span><span class="hl-identifier">parameterName</span></pre>
            </div>
        </div>
        <p>在方法声明中，在指定参数类型后加一个省略号(...) 。
        </p><p>一个方法中只能指定一个可变参数，它必须是方法的最后一个参数。任何普通的参数必须在它之前声明。
    </p>
        <h3>实例</h3>
        <div class="example">
            <h2 class="example">VarargsDemo.java 文件代码：</h2>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">VarargsDemo</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-code"> </span><span class="hl-identifier">args</span><span class="hl-brackets">[</span><span class="hl-brackets">]</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
        </span><span class="hl-comment">//</span><span class="hl-comment"> 调用可变参数的方法</span><span class="hl-comment"></span><span class="hl-code">
        </span><span class="hl-identifier">printMax</span><span class="hl-brackets">(</span><span class="hl-number">34</span><span class="hl-code">, </span><span class="hl-number">3</span><span class="hl-code">, </span><span class="hl-number">3</span><span class="hl-code">, </span><span class="hl-number">2</span><span class="hl-code">, </span><span class="hl-number">56</span><span class="hl-number">.5</span><span class="hl-brackets">)</span><span class="hl-code">;
        </span><span class="hl-identifier">printMax</span><span class="hl-brackets">(</span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-types">double</span><span class="hl-brackets">[</span><span class="hl-brackets">]</span><span class="hl-brackets">{</span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-number">2</span><span class="hl-code">, </span><span class="hl-number">3</span><span class="hl-brackets">}</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-brackets">}</span><span class="hl-code">

    </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">printMax</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-types">double</span><span class="hl-code">... </span><span class="hl-identifier">numbers</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
        </span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">numbers</span><span class="hl-code">.</span><span class="hl-identifier">length</span><span class="hl-code"> == </span><span class="hl-number">0</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
            </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">No argument passed</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
            </span><span class="hl-reserved">return</span><span class="hl-code">;
        </span><span class="hl-brackets">}</span><span class="hl-code">

        </span><span class="hl-types">double</span><span class="hl-code"> </span><span class="hl-identifier">result</span><span class="hl-code"> = </span><span class="hl-identifier">numbers</span><span class="hl-brackets">[</span><span class="hl-number">0</span><span class="hl-brackets">]</span><span class="hl-code">;

        </span><span class="hl-reserved">for</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">i</span><span class="hl-code"> = </span><span class="hl-number">1</span><span class="hl-code">; </span><span class="hl-identifier">i</span><span class="hl-code"> &lt;  </span><span class="hl-identifier">numbers</span><span class="hl-code">.</span><span class="hl-identifier">length</span><span class="hl-code">; </span><span class="hl-identifier">i</span><span class="hl-code">++</span><span class="hl-brackets">)</span><span class="hl-brackets">{</span><span class="hl-code">
            </span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">numbers</span><span class="hl-brackets">[</span><span class="hl-identifier">i</span><span class="hl-brackets">]</span><span class="hl-code"> &gt;  </span><span class="hl-identifier">result</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
                </span><span class="hl-identifier">result</span><span class="hl-code"> = </span><span class="hl-identifier">numbers</span><span class="hl-brackets">[</span><span class="hl-identifier">i</span><span class="hl-brackets">]</span><span class="hl-code">;
            </span><span class="hl-brackets">}</span><span class="hl-code">
        </span><span class="hl-brackets">}</span><span class="hl-code">
        </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">The max value is </span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">result</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span></pre>
            </div>
        </div>
        <p>以上实例编译运行结果如下：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="typ">The</span><span class="pln"> max value </span><span class="kwd">is</span><span class="pln"> </span><span class="lit">56.5</span><span class="pln">
</span><span class="typ">The</span><span class="pln"> max value </span><span class="kwd">is</span><span class="pln"> </span><span class="lit">3.0</span></pre>
        <hr><h2>finalize() 方法</h2>

        <p>Java 允许定义这样的方法，它在对象被垃圾收集器析构(回收)之前调用，这个方法叫做 finalize( )，它用来清除回收对象。
        </p><p>例如，你可以使用 finalize() 来确保一个对象打开的文件被关闭了。
    </p><p>在 finalize() 方法里，你必须指定在对象销毁时候要执行的操作。
    </p><p>finalize() 一般格式是：</p>
        <div class="example">
            <div class="example_code">
                <pre class="hl-main"><span class="hl-reserved">protected</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">finalize</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-brackets">{</span><span class="hl-code">
   </span><span class="hl-comment">//</span><span class="hl-comment"> 在这里终结代码</span><span class="hl-comment"></span><span class="hl-code">
</span><span class="hl-brackets">}</span></pre>
            </div>
        </div>
        <p>
            关键字 protected 是一个限定符，它确保 finalize() 方法不会被该类以外的代码调用。
        </p>
        <p>
            当然，Java 的内存回收可以由 JVM 来自动完成。如果你手动使用，则可以使用上面的方法。

        </p>
        <h3>实例</h3>
        <div class="example">
            <h2 class="example">FinalizationDemo.java 文件代码：</h2>
            <div class="example_code">
                <pre class="hl-main"><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">FinalizationDemo</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
  </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-types">static</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-identifier">String</span><span class="hl-brackets">[</span><span class="hl-brackets">]</span><span class="hl-code"> </span><span class="hl-identifier">args</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-identifier">Cake</span><span class="hl-code"> </span><span class="hl-identifier">c1</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Cake</span><span class="hl-brackets">(</span><span class="hl-number">1</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">Cake</span><span class="hl-code"> </span><span class="hl-identifier">c2</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Cake</span><span class="hl-brackets">(</span><span class="hl-number">2</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">Cake</span><span class="hl-code"> </span><span class="hl-identifier">c3</span><span class="hl-code"> = </span><span class="hl-reserved">new</span><span class="hl-code"> </span><span class="hl-identifier">Cake</span><span class="hl-brackets">(</span><span class="hl-number">3</span><span class="hl-brackets">)</span><span class="hl-code">;

    </span><span class="hl-identifier">c2</span><span class="hl-code"> = </span><span class="hl-identifier">c3</span><span class="hl-code"> = </span><span class="hl-reserved">null</span><span class="hl-code">;
    </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">gc</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">; </span><span class="hl-comment">//</span><span class="hl-comment">调用Java垃圾收集器</span><span class="hl-comment"></span><span class="hl-code">
  </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span><span class="hl-code">

</span><span class="hl-reserved">class</span><span class="hl-code"> </span><span class="hl-identifier">Cake</span><span class="hl-code"> </span><span class="hl-reserved">extends</span><span class="hl-code"> </span><span class="hl-identifier">Object</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
  </span><span class="hl-reserved">private</span><span class="hl-code"> </span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">id</span><span class="hl-code">;
  </span><span class="hl-reserved">public</span><span class="hl-code"> </span><span class="hl-identifier">Cake</span><span class="hl-brackets">(</span><span class="hl-types">int</span><span class="hl-code"> </span><span class="hl-identifier">id</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">this</span><span class="hl-code">.</span><span class="hl-identifier">id</span><span class="hl-code"> = </span><span class="hl-identifier">id</span><span class="hl-code">;
    </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">Cake Object </span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">id</span><span class="hl-code"> + </span><span class="hl-quotes">"</span><span class="hl-string">is created</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
  </span><span class="hl-brackets">}</span><span class="hl-code">

  </span><span class="hl-reserved">protected</span><span class="hl-code"> </span><span class="hl-types">void</span><span class="hl-code"> </span><span class="hl-identifier">finalize</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code"> </span><span class="hl-reserved">throws</span><span class="hl-code"> </span><span class="hl-identifier">java</span><span class="hl-code">.</span><span class="hl-identifier">lang</span><span class="hl-code">.</span><span class="hl-identifier">Throwable</span><span class="hl-code"> </span><span class="hl-brackets">{</span><span class="hl-code">
    </span><span class="hl-reserved">super</span><span class="hl-code">.</span><span class="hl-identifier">finalize</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span><span class="hl-code">;
    </span><span class="hl-identifier">System</span><span class="hl-code">.</span><span class="hl-identifier">out</span><span class="hl-code">.</span><span class="hl-identifier">println</span><span class="hl-brackets">(</span><span class="hl-quotes">"</span><span class="hl-string">Cake Object </span><span class="hl-quotes">"</span><span class="hl-code"> + </span><span class="hl-identifier">id</span><span class="hl-code"> + </span><span class="hl-quotes">"</span><span class="hl-string">is disposed</span><span class="hl-quotes">"</span><span class="hl-brackets">)</span><span class="hl-code">;
  </span><span class="hl-brackets">}</span><span class="hl-code">
</span><span class="hl-brackets">}</span></pre>
            </div>
        </div>
        <p>运行以上代码，输出结果如下：</p>
        <pre class="prettyprint prettyprinted" style=""><span class="pln">$ javac </span><span class="typ">FinalizationDemo</span><span class="pun">.</span><span class="pln">java
$ java </span><span class="typ">FinalizationDemo</span><span class="pln">
</span><span class="typ">Cake</span><span class="pln"> </span><span class="typ">Object</span><span class="pln"> </span><span class="lit">1is</span><span class="pln"> created
</span><span class="typ">Cake</span><span class="pln"> </span><span class="typ">Object</span><span class="pln"> </span><span class="lit">2is</span><span class="pln"> created
</span><span class="typ">Cake</span><span class="pln"> </span><span class="typ">Object</span><span class="pln"> </span><span class="lit">3is</span><span class="pln"> created
</span><span class="typ">Cake</span><span class="pln"> </span><span class="typ">Object</span><span class="pln"> </span><span class="lit">3is</span><span class="pln"> disposed
</span><span class="typ">Cake</span><span class="pln"> </span><span class="typ">Object</span><span class="pln"> </span><span class="lit">2is</span><span class="pln"> disposed</span></pre>

    </div>
</div>
</body>
</html>
